package com.usernet.product.web.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.usernet.product.comm.BaseAction;
import com.usernet.product.entity.Admin;
import com.usernet.product.entity.Comment;
import com.usernet.product.utils.LogConstants;
import com.usernet.product.utils.Page;
import com.usernet.product.utils.PageResult;
import com.usernet.product.utils.ProductUtils;
import com.usernet.product.web.form.CommentForm;

public class CommentAction extends BaseAction {

	public ActionForward toList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CommentForm mForm = (CommentForm) form;
		// 查询条件
		String time = mForm.getTime();
		String softName = mForm.getSoftName();

		PageResult<Comment> result = null;
		Page page = new Page();
		// 分页信息
		String pageNo = mForm.getPageNo();
		String pageSize = mForm.getPageSize();
		if (pageSize != null) {
			page.setEveryPage(Integer.parseInt(pageSize.trim()));
		}
		if (pageNo != null) {
			page.setCurrentPage(Integer.parseInt(pageNo.trim()));
		}
		// 条件查询
		if (time != null || softName != null) {
			// 有条件
			result = commentDao.getAllByParams(page, softName, time);
		} else {

			result = commentDao.getAllByPage(page);
		}
		request.setAttribute("form", mForm);
		request.setAttribute("list", result.getContent());
		request.setAttribute("page", result.getPage());
		request.setAttribute("size", result.getSize());

		return mapping.findForward("list");
	}

	public ActionForward doDelete(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		int id = Integer.parseInt(request.getParameter("id"));
		Comment comment = (Comment) commentDao.getById(Comment.class, id);
		if (commentDao.delete(comment)) {
			Admin admin = (Admin) request.getSession().getAttribute("admin");
			// 添加日志
			ProductUtils.addAdminLog(request, admin.getLoginName(), String
					.format(LogConstants.delete_comment, new Object[] {
							comment.getId(), comment.getInfo() }), null);
		}
		return mapping.findForward("other");
	}

}
